package com.example.mapper;

import com.example.bean.Student;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @program: mybatis_maven
 * @description:
 * @author: zhanghz001
 * @create: 2020-11-10 19:22
 **/
public interface StudentMapper3 {
    //查询全部
    @Select("select *\n" +
            "from student;")
    List<Student> selectAll();

    //根据id查询
    @Select("select *\n" +
            "from student s\n" +
            "where s.id= #{id};")
    Student selectById(Integer id);

    //新增数据
    @Insert("insert into student (id,NAME, age)\n" +
            "values ( #{id}, #{name}, #{age});")
    Integer insert(Student stu);

    //修改数据
    @Update("update student\n" +
            "set NAME =  #{name},\n" +
            "    age=#{age}\n" +
            "where id = 1;")
    List<Student> update(Student stu);

    //删除数据
    @Delete("delete from student where  id=#{id};")
    Integer delete(Integer id);

    //根据cid查询
    @Select("select *\n" +
            "from student s\n" +
            "where s.cid= #{cid};")
    List<Student> selectByCid(Integer cid);

    // 查询
    @Select("select id, NAME, age, cid\n" +
            "from student;")
    @Results(id = "student_course", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "NAME"),
            @Result(property = "age", column = "age"),
            @Result(property = "courses", column = "id", javaType = List.class
                    , many = @Many(
                    select = "com.example.mapper.CourseMapper.selectBySid"
            ))})
    List<Student> selectIncludeCourses();
}
